const app = getApp()
const host = 'http://localhost:7001';
const online = 'https://www.leotian.cn';

const request = (url, options) => {
  const token = wx.getStorageSync('token');
  const BpeSessionId= wx.getStorageSync('BpeSessionId');
  const isCreatePayOrder = url.includes('/api/pay');
  if(!url.includes('http')){
    url =`${online}${url}`
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url,
      method: options.method,
      data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
      header: {
        'Content-Type': 'application/json; charset=UTF-8',
        'dauthorization': token,
        Cookie:`BpeSessionId=${BpeSessionId}`
      },
      success(request) {
        const {
          statusCode,
          data
        } = request;
        if (statusCode === 200) {
          resolve(data)
        }
      },
      fail(error) {
        reject(error.data)
        wx.showToast({
          title: error.data || '网络异常',
          icon: 'none',
          duration: 2000
        })
      }
    })
  })
}

export const get = (url, options = {}) => {
  return request(url, {
    method: 'GET',
    data: options
  })
}
export const post = (url, options) => {
  return request(url, {
    method: 'POST',
    data: options
  })
}
export const put = (url, options) => {
  return request(url, {
    method: 'PUT',
    data: options
  })
}
// 不能声明DELETE（关键字）
export const remove = (url, options) => {
  return request(url, {
    method: 'DELETE',
    data: options
  })
}